package com.qiankan.qmsj.fragment;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qiankan.qmsj.R;
import com.qiankan.qmsj.UserInfo;
import com.qiankan.qmsj.activity.ApplyRecordActivity;
import com.qiankan.qmsj.activity.FastSignInActivity;
import com.qiankan.qmsj.activity.LogInActivity;
import com.qiankan.qmsj.activity.PersionInfoActivity;
import com.qiankan.qmsj.activity.SettingActivity;
import com.qiankan.qmsj.activity.WebActivity;
import com.qiankan.qmsj.db.UserDBHelper;
import com.qiankan.qmsj.utils.AsyncLoadImage;
import com.qiankan.qmsj.utils.MobileUtil;
import com.qiankan.qmsj.widget.TipToast;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/8/19.
 */

public class MyFragment extends BaseFragment {
    @BindView(R.id.ll_login_register)
    LinearLayout layoutLoginRegister;
    @BindView(R.id.username)
    TextView mUserName;
    @BindView(R.id.img_head)
    ImageView imgHeader;

    @Override
    public int getContentViewId() {
        return R.layout.my_fragment;
    }

    @Override
    public void onResume() {
        super.onResume();
        updateView();
    }

    @OnClick(R.id.rl_applyRecord)
    public void onClickApplyRecord(View view) {
        if (checkLogIn()) {
            startActivity(ApplyRecordActivity.newIntent(getActivity()));
        } else {
            startActivity(LogInActivity.newIntent(getActivity()));
        }
    }

    @OnClick(R.id.rl_personalInfo)
    public void onClickPersonalInfo(View view) {
        if (checkLogIn()) {
            startActivity(PersionInfoActivity.newIntent(getActivity()));
        } else {
            startActivity(LogInActivity.newIntent(getActivity()));
        }
    }

    @OnClick(R.id.rl_question)
    public void onClickQuestion(View view) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("hideShare", true);
        bundle.putString("url", "http://www.baidu.com");
        startActivity(WebActivity.newIntent(getActivity(), bundle));
    }

    @OnClick(R.id.rl_online_service)
    public void onClickService(View view) {
        if (MobileUtil.isQQClientAvailable(getActivity())) {
            //Android打开手机QQ与指定用户聊天界面
            String url = "mqqwpa://im/chat?chat_type=wpa&uin=3349524544";
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

        } else {
            TipToast.toast(getActivity(), "请下载安装QQ！");
        }
    }

    @OnClick(R.id.rl_setting)
    public void onClickSetting(View view) {
        if (checkLogIn()) {
            startActivity(SettingActivity.newIntent(getActivity()));
        } else {
            Intent intent = LogInActivity.newIntent(getActivity());
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
    }

    @OnClick(R.id.tv_login)
    public void onClickLogIn(View view) {
        startActivity(LogInActivity.newIntent(getActivity()));
    }

    @OnClick(R.id.tv_signIn)
    public void onClickSignIn(View view) {
        startActivity(FastSignInActivity.newIntent(getActivity()));
    }

    private boolean checkLogIn() {
        return UserDBHelper.getInstance(getActivity()).getUserInfo() != null;
    }

    /*@Subscribe(threadMode = ThreadMode.MAIN)
    public void onUpdateMyFragment(UpdateMyFragmentEvent event) {
        updateView();
    }*/

    private void updateView() {
        UserInfo userInfo = UserDBHelper.getInstance(getActivity()).getUserInfo();
        if (userInfo != null) {
            if (!TextUtils.isEmpty(userInfo.getHead())) {
                AsyncLoadImage.loadImageRoundWithSign(userInfo.getHead(), imgHeader, R.mipmap.wd_mrtx);
            }
            if (!TextUtils.isEmpty(userInfo.getPhone())) {
                mUserName.setText("用户" + userInfo.getPhone().replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
                mUserName.setVisibility(View.VISIBLE);
                layoutLoginRegister.setVisibility(View.INVISIBLE);
            }
        } else {
            mUserName.setVisibility(View.INVISIBLE);
            layoutLoginRegister.setVisibility(View.VISIBLE);
            AsyncLoadImage.loadImage("", imgHeader, R.mipmap.wd_mrtx);
        }
    }
}